var actbill = require('../../../xc/model/pro_contents')
var actbillclass = require('../../../xc/model/pro_class')
var actprothem = require('../../../xc/sys/prothem')

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],
  /**
   * 页面的初始数据
   */
  data: {
    op: "", //search显示搜索页
    navtype: "top", //top,left
    modellist: [],
    treeclass: null,
    actpidx: -1,
    actspid: 0,
    actpid: 0,
    seachval: "",
    proconfig: null,
    childrenclass: [{
      id: 1,
      "name": "小分类1",
      simg: "http://cx002exrds.7mx7.com/images/5/2020/07/R0U81Uchc8puZK00801E16L681E66K.jpg"
    }, ],
    hotsearch: [],

  },
  // 加购成功事件
  aftercart(e) {
    this.selectComponent("#footer").loadcardlength()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onCancel: function (ev) {
    var that = this;
    this.setData({
      seachval: ""
    })
    this.getpagelist(that, 1)
  },
  onSearch: function (ev) {
    this.setData({
      seachval: ev.detail
    })
    if (ev.detail) {
      var searchkeys = wx.getStorageSync('searchkeys')
      if (!searchkeys) {
        searchkeys = [];
      } else {
        searchkeys = JSON.parse(searchkeys)
      }
      searchkeys.unshift(ev.detail)
      searchkeys = this.unique(searchkeys)
      wx.setStorageSync('searchkeys', JSON.stringify(searchkeys))
    }

    var that = this;
    this.getpagelist(that, 1)
  },

  tabschangclass: function (ev) {
    // console.log("ev")

    // console.log(ev)
    this.changclass({
      detail: ev.detail.name
    })

  },
  getactpidx(actpid) {
    var treeclass = this.data.treeclass;
    var pidlist = null
    if (treeclass.level == "1") {
      pidlist = treeclass.modellist;
    } else {
      pidlist = treeclass.tree;
    }
    console.log("pidlist")
    console.log(pidlist)
    console.log(actpid)

    for (var i in pidlist) {
      if (pidlist[i].id == actpid) {
        var index = i * 1 + 1
        this.setData({
          activeKey: index,
          actpidx: index
        })
      }
    }
    console.log(pidlist)
  },
  changclass: function (ev) {

    var treeclass = this.data.treeclass;
    var pidlist = null
    if (treeclass.level == "1") {
      pidlist = treeclass.modellist;
    } else {
      pidlist = treeclass.tree;
    }
    this.data.actpidx = ev.detail
    if (this.data.actpidx == 0) {
      this.data.actpid = 0
    } else {
      this.data.actpid = pidlist[(this.data.actpidx - 1)].id
    }
    this.setData({
      actpidx: this.data.actpidx,
      actspid: 0
    })
    var that = this;
    this.getpagelist(that, 1)
  },
  afteractspid(ev) {
    this.setData({
      actspid: ev.detail
    })
    var that = this
    this.getpagelist(that, 1)
  },

  xcloaddata: function (options, config) {
    // console.log("config")
    // console.log(config)

    //一级 bpid=id spid=0 二级 bpid 大分类 spid小分类
    if (options.bpid) { //大分类
      this.data.actpid = options.bpid;
    }
    if (options.spid) { //小分类
      this.data.actspid = options.spid;
    }
    console.log("this.data.actpid")
    console.log(this.data.actpid)

    var configupdate = {};
    if (config.proconfig && config.proconfig.hotsearch) {
      configupdate['hotsearch'] = config.proconfig.hotsearch;
    }
    if (config.rebate) {
      configupdate['configrebate'] = config.rebate;
    } else {
      configupdate['configrebate'] = {
        status: "-1"
      };
    }
    if (config.order) {
      if (config.order.cart) {
        configupdate['cartstatus'] = config.order.cart;
      } else {
        configupdate['cartstatus'] = 1
      }
    }
    if (options['op']) {
      configupdate['op'] = options['op'];
    }
    this.setData(configupdate)

    if (config.prothem && config.prothem.prolist) {
      var proconfig = config.prothem.prolist.proconfig;
      var customStyle = config.prothem.prolist.customStyle;
      console.log("pppppppppppp")
      console.log(customStyle)

      var popconfig = config.prothem.popup.proconfig;
      var popCustomStyle = config.prothem.popup.customStyle;
      this.setData({
        proconfig: proconfig,
        navtype: proconfig.left.navmode,
        customStyle: customStyle,
        popconfig: popconfig,
        popCustomStyle: popCustomStyle
      })
    }




    var that = this
    var strwhere = {}
    strwhere["status"] = 1
    that.getpagelist(that, 1, strwhere)
    //加载全部分类
    actbillclass.apiall().then(item => {
      that.setData({
        treeclass: item.obj,
        actpidx: 0
      })

      // console.log("dsff")
      // console.log(that.data.actspid)
      // if(that.data.treeclass.level==1){
      //    that.setData({
      //     actspid:options.bpid,
      //     actpid:options.spid
      //    })
      // }
      if (that.data.actpid) {
        that.getactpidx(that.data.actpid)
      }
      console.log(that.data.actspid)
      if (that.data.actspid > 0) {
        if (that.data.navtype == 'left') {
          console.log(that.data.actspid)
          that.selectComponent("#protopnav").setactcid(that.data.actspid)
        } else {
          that.selectComponent("#imagenav").setactcid(that.data.actspid)
        }
      }

    })



  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere = {};
      }
      strwhere["protype"] = 2;

      strwhere["status"] = 1;
      strwhere["bpid"] = this.data.actpid;
      strwhere["spid"] = this.data.actspid;
      strwhere["seachval"] = this.data.seachval;
      that.setData({
        modellist: []
      });
    }
    actbill.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};

      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {

        for (var a in result["obj"]["modellist"]) {
          if (result["obj"]["modellist"][a]["snapshot"]) {
            result["obj"]["modellist"][a]["snapshot"] = JSON.parse(result["obj"]["modellist"][a]["snapshot"])
          }
        }
        if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        } else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      wx.stopPullDownRefresh();
      that.setData(updatemdoel);


    });
  },
  addmore: function () {
    this.data.modellist.push(1)
    this.setData({
      modellist: this.data.modellist
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.selectComponent("#footer").loadcardlength()
  },
  buy: function (ev) {
    var that = this
    var dataset = ev.detail.dataset
    var item = dataset.item
    console.log(item)


    // 1加购 2立即购买 3两种
    // console.log(item)
    // if (item.contrebate && item.tagsids.indexOf("-1") != -1 && this.data.configrebate['status'] == 1) {
    //   // console.log("ddd")
    //   var option = {
    //     aid: -1001,
    //     atype: -1001
    //   }
    //   actbill.showattrchose(that, item.id, null, 2, option)

    // } else {
    //   if (this.data.cartstatus == '1') {
    //     actbill.showattrchose(that, item.id, null, 3)
    //   } else {
    //     actbill.showattrchose(that, item.id, null, 2)
    //   }
    // }

    var that = this
    // var option = {protype:2}; //团长产品
    var option = {protype:2,clubid:item.shopid,clubmode:item.autarky==1?'4':'2'}; //团长产品
    console.log(ev.currentTarget.dataset.op)
    actbill.showattrchose(that, item.id, that.data.selected, ev.currentTarget.dataset.op, option)
 
    // var dataset = ev.detail.dataset;

  },
  attrchoose: function (e) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    this.getpagelist(that, -1)

  },
})